package com.brokendust.client.api.module.item;

import com.brokendust.client.api.util.LocationUtil;
import com.brokendust.client.config.module.item.ItemEntityJson;
import com.mojang.blaze3d.matrix.MatrixStack;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.IRenderTypeBuffer;
import net.minecraft.client.renderer.ItemRenderer;
import net.minecraft.client.renderer.model.IBakedModel;
import net.minecraft.client.renderer.model.ItemCameraTransforms;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.vector.Vector3f;

public class ItemEntityShow {
    ItemStack itemStack;
    float scale;
    float rotateX;
    float rotateY;
    float rotateZ;
    int autoRotationX;
    int autoRotationY;
    int autoRotationZ;
    private ItemRenderer itemRenderer;
    private String uuidString;
    private double locationHeight;
    private double locationAngle;
    private double locationDistance;

    public ItemEntityShow(ItemEntityJson itemEntityJson) {
        this.itemRenderer = Minecraft.getInstance().getItemRenderer();
        this.itemStack = itemEntityJson.getItemStack();
        this.uuidString = itemEntityJson.getUuidString();
        this.scale = itemEntityJson.getScale();
        this.locationHeight = itemEntityJson.getLocationHeight();
        this.locationAngle = itemEntityJson.getLocationAngle();
        this.locationDistance = itemEntityJson.getLocationDistance();
        this.rotateX = itemEntityJson.getRotateX();
        this.rotateY = itemEntityJson.getRotateY();
        this.rotateZ = itemEntityJson.getRotateZ();
        this.autoRotationX = itemEntityJson.getAutoRotationX();
        this.autoRotationY = itemEntityJson.getAutoRotationY();
        this.autoRotationZ = itemEntityJson.getAutoRotationZ();
    }

    public void renderItem(PlayerEntity player, MatrixStack matrixStackIn, IRenderTypeBuffer bufferIn) {
        String playerUUIDString = player.getStringUUID();
        if (!this.uuidString.equals(playerUUIDString)) {
            return;
        }
        if (this.rotateX >= 360.0f) {
            this.rotateX = 0.0f;
        }
        if (this.rotateY >= 360.0f) {
            this.rotateY = 0.0f;
        }
        if (this.rotateZ >= 360.0f) {
            this.rotateZ = 0.0f;
        }
        double lookAngle = (player.getRotationVector().y - 90.0f + this.locationAngle) % 360.0;
        double[] lookAngleLocation = LocationUtil.rotateXYZ(0.0, lookAngle, 0.0, new double[]{0.0, 0.0, 0.0}, this.locationDistance);
        matrixStackIn.pushPose();
        matrixStackIn.scale(this.scale, this.scale, this.scale);
        matrixStackIn.translate(lookAngleLocation[0], this.locationHeight, lookAngleLocation[1]);
        if (this.rotateX > 0.0f) {
            matrixStackIn.mulPose(Vector3f.XP.rotationDegrees(this.rotateX));
        } else {
            matrixStackIn.mulPose(Vector3f.XN.rotationDegrees(this.rotateX * -1.0f));
        }
        if (this.rotateY > 0.0f) {
            matrixStackIn.mulPose(Vector3f.YP.rotationDegrees(this.rotateY));
        } else {
            matrixStackIn.mulPose(Vector3f.YN.rotationDegrees(this.rotateY * -1.0f));
        }
        if (this.rotateZ > 0.0f) {
            matrixStackIn.mulPose(Vector3f.ZP.rotationDegrees(this.rotateZ));
        } else {
            matrixStackIn.mulPose(Vector3f.ZN.rotationDegrees(this.rotateZ * -1.0f));
        }
        IBakedModel ibakedmodel = this.itemRenderer.getModel(this.itemStack, player.getCommandSenderWorld(), player);
        this.itemRenderer.render(this.itemStack, ItemCameraTransforms.TransformType.NONE, false, matrixStackIn, bufferIn, 15728640, 0, ibakedmodel.getBakedModel());
        matrixStackIn.popPose();
        if (this.autoRotationX > 0) {
            this.rotateX += this.autoRotationX;
        }
        if (this.autoRotationY > 0) {
            this.rotateY += this.autoRotationY;
        }
        if (this.autoRotationZ > 0) {
            this.rotateZ += this.autoRotationZ;
        }
    }
}
